package com.stoken.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.stoken.dao.AchievementListDao;
import com.stoken.dao.MemberAchievementDao;
import com.stoken.dao.MemberBaseInfoDao;
import com.stoken.dao.MemberBonusListDao;
import com.stoken.dao.MemberRegisterInfoDao;
import com.stoken.dao.MemberShopWalletDao;
import com.stoken.dao.MemberWalletDao;
import com.stoken.domain.AchievementList;
import com.stoken.domain.MemberAchievement;
import com.stoken.domain.MemberBonusList;
import com.stoken.domain.MemberRegisterInfo;
import com.stoken.domain.MemberShopWallet;
import com.stoken.domain.MemberWallet;
import com.stoken.service.BonusCalculationService;
import com.stoken.web.StokenUtils;

/**
 * 奖金计算
 * 
 * @author Fred
 * 
 */
@Service("bonusCalculation")
public class BonusCalculationServiceImpl implements BonusCalculationService {

	private WebApplicationContext webAppContext;

	private List<MemberRegisterInfo> activeMembers;

	@Autowired
	private MemberRegisterInfoDao memberRegisterInfoDao;

	@Autowired
	private MemberAchievementDao memberAchievementDao;

	@Autowired
	private MemberBonusListDao memberBonusListDao;

	@Autowired
	private MemberBaseInfoDao memberBaseInfoDao;

	@Autowired
	private MemberWalletDao memberWalletDao;

	@Autowired
	private MemberShopWalletDao memberWalletShopDao;

	@Autowired
	private AchievementListDao achievemntListdao;

	private List<MemberAchievement> hitMembers;

	public BonusCalculationServiceImpl() {
		webAppContext = ContextLoader.getCurrentWebApplicationContext();
	}

	public void startCalculation() {
		init();
		countActiveMembers();
	}

	private void init() {
		webAppContext = ContextLoader.getCurrentWebApplicationContext();
		// 得到会员业绩表Dao层
		memberRegisterInfoDao = (MemberRegisterInfoDao) webAppContext
				.getBean("memberRegisterInfoDao");
		memberAchievementDao = (MemberAchievementDao) webAppContext
				.getBean("memberAchievementDao");
		memberBonusListDao = (MemberBonusListDao) webAppContext
				.getBean("memberBonusListDao");
		memberBaseInfoDao = (MemberBaseInfoDao) webAppContext
				.getBean("memberBaseInfoDao");
		memberWalletDao = (MemberWalletDao) webAppContext
				.getBean("memberWalletDao");
		achievemntListdao = (AchievementListDao) webAppContext
				.getBean("achievementListDao");
		memberWalletShopDao = (MemberShopWalletDao) webAppContext
				.getBean("memberWalletShopDao");

	}

	private void countActiveMembers() {

		activeMembers = memberRegisterInfoDao.countActiveMember();
		// 计算对碰业绩
		for (MemberRegisterInfo mr : activeMembers) {
			calculationBonus(mr);
		}

	}

	/**
	 * 计算直接推荐奖金
	 * 
	 * @param memberRegisterInfo
	 */
	private void calculationSellBonus(MemberRegisterInfo memberRegisterInfo) {

		double sellBonus = StokenUtils
				.getAchievementByClassLevel(memberRegisterInfo
						.getRegisterGrade())
				* StokenUtils.UNIT * StokenUtils.REFERRALBONUS;
		String commendUsername = memberRegisterInfo
				.getRegisterCommendUsername().getBaseUsername();
		// 获得会员的电子钱包
		MemberWallet memw = memberWalletDao
				.getMemberWalletByUserName(commendUsername);
		MemberShopWallet memwShop = memberWalletShopDao
				.getMemberWalletByUserName(commendUsername);
		double acut = sellBonus
				- (sellBonus * StokenRule.CONSUMPTION_WITHDRAWALSFEE);
		memw.setWalletRemain(memw.getWalletRemain() + acut);
		memwShop.setShopWalletBalance((sellBonus * StokenRule.CONSUMPTION_WITHDRAWALSFEE)
				+ memwShop.getShopWalletBalance());
		// 奖金汇总表
		AchievementList achievmentList = achievemntListdao
				.getAchievemntListByAccountAndDate(
						StokenRule.getCalculateBonusDate(), commendUsername);
		achievmentList.setSellBonus(achievmentList.getSellBonus() + sellBonus);
		achievmentList.setConsumptionBonus(achievmentList.getConsumptionBonus()
				+ (sellBonus * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
		achievmentList.setActualBonus(achievmentList.getActualBonus() + acut);
		achievemntListdao.update(achievmentList);

		// 奖金详细列表
		MemberBonusList memberBonusList = new MemberBonusList(); // 奖金详细列表
		memberBonusList.setBaseUsername(memberBaseInfoDao.find("baseUsername",
				commendUsername));
		memberBonusList.setBonusBringUsername(memberBaseInfoDao.find(
				"baseUsername", memberRegisterInfo.getBaseUsername()));
		memberBonusList.setBonusSort(1);
		memberBonusList.setBonusBringTime(new Date());
		memberBonusList.setWalletRemain(memw.getWalletRemain());
		memberBonusList.setBonusBringMoney(sellBonus);
		memberWalletDao.update(memw);
		memberWalletShopDao.update(memwShop);
		memberBonusListDao.save(memberBonusList);
	}

	/**
	 * 计算领导奖金
	 * 
	 * @param memberAchievement
	 * @param hitBonus
	 */
	private void calculationLeadershipBonus(
			MemberRegisterInfo memberRegisterInfo, double hitBonus) {
		int i = 0;
		String bringUsername = memberRegisterInfo.getBaseUsername();
		while ((memberRegisterInfo.getRegisterCommendUsername() != null)
				&& i < 4) {
			MemberRegisterInfo leaderMemberRegisterInfo = memberRegisterInfoDao
					.getMbRegisterByBaseName(memberRegisterInfo
							.getRegisterCommendUsername().getBaseUsername());
			MemberWallet mwllet = memberWalletDao
					.getMemberWalletByUserName(leaderMemberRegisterInfo
							.getBaseUsername());
			MemberShopWallet mwlletShop = memberWalletShopDao
					.getMemberWalletByUserName(leaderMemberRegisterInfo
							.getBaseUsername());
			// 奖金详细列表
			MemberBonusList memberBonusList = new MemberBonusList(); // 奖金详细列表
			memberBonusList
					.setBaseUsername(memberBaseInfoDao.find("baseUsername",
							leaderMemberRegisterInfo.getBaseUsername()));
			memberBonusList.setBonusBringUsername(memberBaseInfoDao.find(
					"baseUsername", bringUsername));
			memberBonusList.setBonusSort(3);
			memberBonusList.setBonusBringTime(new Date());

			if (i == 0) {
				double leadershipBonusFirst = hitBonus
						* StokenUtils.LEADERSHIPBONUS_FIRST; // 领导一代奖金
				double shopBonus = leadershipBonusFirst
						* StokenRule.CONSUMPTION_WITHDRAWALSFEE;
				mwllet.setWalletRemain(mwllet.getWalletRemain()
						+ (leadershipBonusFirst - shopBonus));
				mwlletShop.setShopWalletBalance(shopBonus
						+ mwlletShop.getShopWalletBalance());

				// 奖金汇总表
				AchievementList achievmentList = achievemntListdao
						.getAchievemntListByAccountAndDate(
								StokenRule.getCalculateBonusDate(),
								leaderMemberRegisterInfo.getBaseUsername());
				achievmentList.setLeadershipBonus(achievmentList.getLeadershipBonus()+leadershipBonusFirst);
				achievmentList.setConsumptionBonus(achievmentList
						.getConsumptionBonus() + (shopBonus));
				achievmentList.setActualBonus(achievmentList.getActualBonus()
						+ (leadershipBonusFirst - shopBonus));
				achievemntListdao.update(achievmentList);

				memberBonusList.setWalletRemain(mwllet.getWalletRemain());
				memberBonusList.setBonusBringMoney(leadershipBonusFirst);
			} else if (i == 1) {
				double leadershipBonusSecond = hitBonus
						* StokenUtils.LEADERSHIPBONUS_SECOND; // 领导二代奖金
				double shopBonus = leadershipBonusSecond
						* StokenRule.CONSUMPTION_WITHDRAWALSFEE;
				mwllet.setWalletRemain(mwllet.getWalletRemain()
						+ (leadershipBonusSecond - shopBonus));
				mwlletShop.setShopWalletBalance(shopBonus
						+ mwlletShop.getShopWalletBalance());
				// 奖金汇总表
				AchievementList achievmentList = achievemntListdao
						.getAchievemntListByAccountAndDate(
								StokenRule.getCalculateBonusDate(),
								leaderMemberRegisterInfo.getBaseUsername());
				achievmentList.setLeadershipBonus(achievmentList.getLeadershipBonus()+leadershipBonusSecond);
				achievmentList
						.setConsumptionBonus(achievmentList
								.getConsumptionBonus()
								+ (leadershipBonusSecond * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
				achievmentList
						.setActualBonus(achievmentList.getActualBonus()
								+ (leadershipBonusSecond - (leadershipBonusSecond * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
				achievemntListdao.update(achievmentList);

				memberBonusList.setWalletRemain(mwllet.getWalletRemain());
				memberBonusList.setBonusBringMoney(leadershipBonusSecond);
			} else if (i == 2) {
				double leadershipBonusThird = hitBonus
						* StokenUtils.LEADERSHIPBONUS_THIRD; // 领导三代奖金
				double shopBonus = leadershipBonusThird
						* StokenRule.CONSUMPTION_WITHDRAWALSFEE;
				mwllet.setWalletRemain(mwllet.getWalletRemain()
						+ (leadershipBonusThird - shopBonus));
				mwlletShop.setShopWalletBalance(shopBonus
						+ mwlletShop.getShopWalletBalance());
				// 奖金汇总表
				AchievementList achievmentList = achievemntListdao
						.getAchievemntListByAccountAndDate(
								StokenRule.getCalculateBonusDate(),
								leaderMemberRegisterInfo.getBaseUsername());
				achievmentList.setLeadershipBonus(achievmentList.getLeadershipBonus()+leadershipBonusThird);
				achievmentList
						.setConsumptionBonus(achievmentList
								.getConsumptionBonus()
								+ (leadershipBonusThird * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
				achievmentList
						.setActualBonus(achievmentList.getActualBonus()
								+ (leadershipBonusThird - (leadershipBonusThird * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
				achievemntListdao.update(achievmentList);

				memberBonusList.setWalletRemain(mwllet.getWalletRemain());
				memberBonusList.setBonusBringMoney(leadershipBonusThird);
			} else if (i == 3) {
				double leadershipBonusFourth = hitBonus
						* StokenUtils.LEADERSHIPBONUS_FOURTH; // 领导三代奖金
				double shopBonus = leadershipBonusFourth
						* StokenRule.CONSUMPTION_WITHDRAWALSFEE;
				mwllet.setWalletRemain(mwllet.getWalletRemain()
						+ leadershipBonusFourth);
				mwlletShop.setShopWalletBalance(shopBonus
						+ (mwlletShop.getShopWalletBalance() - shopBonus));
				// 奖金汇总表
				AchievementList achievmentList = achievemntListdao
						.getAchievemntListByAccountAndDate(
								StokenRule.getCalculateBonusDate(),
								leaderMemberRegisterInfo.getBaseUsername());
				achievmentList.setLeadershipBonus(achievmentList.getLeadershipBonus()+leadershipBonusFourth);
				achievmentList
						.setConsumptionBonus(achievmentList
								.getConsumptionBonus()
								+ (leadershipBonusFourth * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
				achievmentList
						.setActualBonus(achievmentList.getActualBonus()
								+ (leadershipBonusFourth - (leadershipBonusFourth * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
				achievemntListdao.update(achievmentList);

				memberBonusList.setWalletRemain(mwllet.getWalletRemain());
				memberBonusList.setBonusBringMoney(leadershipBonusFourth);
			}
			memberRegisterInfo = null;
			memberRegisterInfo = leaderMemberRegisterInfo;
			memberWalletDao.update(mwllet);
			memberWalletDao.save(mwllet);
			memberBonusListDao.save(memberBonusList);
			i = i+1;
		}

	}

	/**
	 * 计算对碰业绩
	 * 
	 * @param mrinfo
	 */
	private void calculationBonus(MemberRegisterInfo mrinfo) {
		// 得到会员的层级位置
		String level = mrinfo.getRegisterLevel();
		String[] levels = level.split("-");
		int layer = Integer.valueOf(levels[0]);
		int location = Integer.valueOf(levels[1]);
		int coutlevel = StokenUtils.getAchievementByClassLevel(mrinfo
				.getRegisterGrade());

		while ((layer - 1) > 0) {

			if (location % 2 == 0) { // 表示是上一级右区
				StringBuilder classLevel = new StringBuilder()
						.append(layer - 1).append("-").append(location / 2);
				MemberRegisterInfo memberR = memberRegisterInfoDao
						.getMbRegisterByLevel(classLevel.toString());

				MemberAchievement memberAchievement = memberAchievementDao
						.getMemberAchievementByBaseName(memberR
								.getBaseUsername());
				// 会员最高可以获取的对碰奖金
				double maxBonus = StokenUtils.getMaxHitBonus(memberAchievement
						.getRegisterGrade());
				// 获得会员的电子钱包
				MemberWallet memw = memberWalletDao
						.getMemberWalletByUserName(memberR.getBaseUsername());
				// 获得消费基金账户
				MemberShopWallet memwShop = memberWalletShopDao
						.getMemberWalletByUserName(memberR.getBaseUsername());
				if (memberAchievement.getAchievementLeftTotal() > memberAchievement
						.getAchievementRightTotal()) {
					// 奖金详细列表
					MemberBonusList memberBonusList = new MemberBonusList(); // 奖金详细列表
					memberBonusList.setBaseUsername(memberBaseInfoDao.find(
							"baseUsername", memberR.getBaseUsername()));
					memberBonusList.setBonusBringUsername(memberBaseInfoDao
							.find("baseUsername", mrinfo.getBaseUsername()));
					memberBonusList.setBonusSort(4);
					memberBonusList.setBonusBringTime(new Date());

					int surplusLevel = memberAchievement
							.getAchievementLeftTotal()
							- memberAchievement.getAchievementRightTotal();
					// 该会员已经产生的奖金
					double hitBonusBefore = memberAchievement
							.getAchievementHit()
							* StokenUtils.UNIT
							* StokenUtils.HITBOUNS;
					double actual = 0;
					// 奖金汇总表
					AchievementList achievmentList = achievemntListdao
							.getAchievemntListByAccountAndDate(
									StokenRule.getCalculateBonusDate(),
									memberR.getBaseUsername());
					if (surplusLevel - coutlevel >= 0) {
						double hitBonusOnes = StokenUtils.UNIT * coutlevel
								* StokenUtils.HITBOUNS; // 改成会员产生的对碰奖金
						// 判断对碰奖金是否封顶
						if (hitBonusBefore < maxBonus) {
							if (hitBonusOnes <= (maxBonus - hitBonusBefore)) {
								actual = hitBonusOnes;
								memwShop.setShopWalletBalance(memwShop
										.getShopWalletBalance()
										+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
								memw.setWalletRemain(memw.getWalletRemain()
										+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
								// 计算领导奖金
								calculationLeadershipBonus(
										memberR,
										(actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
							} else {
								actual = maxBonus - hitBonusBefore;
								memwShop.setShopWalletBalance(memwShop
										.getShopWalletBalance()
										+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
								memw.setWalletRemain(memw.getWalletRemain()
										+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
								// 计算领导奖金
								calculationLeadershipBonus(
										memberR,
										(actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
							}

							achievmentList.setHillBonus(achievmentList
									.getHillBonus() + actual);
							achievmentList
									.setConsumptionBonus(achievmentList
											.getConsumptionBonus()
											+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
							achievmentList
									.setActualBonus(achievmentList
											.getActualBonus()
											+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));

						} else {// 封顶

						}

						memberAchievement.setAchievementHit(memberAchievement
								.getAchievementHit() + coutlevel);
						memberBonusList.setBonusBringMoney(hitBonusOnes);
						memberBonusList.setWalletRemain(memw.getWalletRemain());

						memberAchievement.setAchievementLeftRemain(surplusLevel
								- coutlevel);
					} else {
						double hitBonusOnes = StokenUtils.UNIT * surplusLevel
								* StokenUtils.HITBOUNS; // 改成会员产生的对碰奖金
						// 判断对碰奖金是否封顶
						if (hitBonusBefore < maxBonus) {
							if (hitBonusOnes <= (maxBonus - hitBonusBefore)) {
								actual = hitBonusOnes;
								memw.setWalletRemain(memw.getWalletRemain()
										+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
								memwShop.setShopWalletBalance(memwShop
										.getShopWalletBalance()
										+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
								// 计算领导奖金
								calculationLeadershipBonus(
										memberR,
										(actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
							} else {
								actual = maxBonus - hitBonusBefore;
								memw.setWalletRemain(memw.getWalletRemain()
										+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
								memwShop.setShopWalletBalance(memwShop
										.getShopWalletBalance()
										+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
								// 计算领导奖金
								calculationLeadershipBonus(
										memberR,
										(actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
							}

							achievmentList.setHillBonus(achievmentList
									.getHillBonus() + actual);
							achievmentList
									.setConsumptionBonus(achievmentList
											.getConsumptionBonus()
											+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
							achievmentList
									.setActualBonus(achievmentList
											.getActualBonus()
											+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));

						} else {// 封顶

						}
						memberAchievement.setAchievementHit(memberAchievement
								.getAchievementHit() + surplusLevel);
						memberBonusList.setBonusBringMoney(hitBonusOnes);
						memberBonusList.setWalletRemain(memw.getWalletRemain());
						memberAchievement.setAchievementLeftRemain(0);
						memberAchievement.setAchievementRightRemain(coutlevel
								- surplusLevel);
					}
					memberBonusListDao.save(memberBonusList);
					achievemntListdao.update(achievmentList);
					memberWalletDao.update(memw);
					memberWalletShopDao.update(memwShop);

				} else if (memberAchievement.getAchievementLeftTotal() <= memberAchievement
						.getAchievementRightTotal()) {
					memberAchievement
							.setAchievementRightRemain(memberAchievement
									.getAchievementRightRemain() + coutlevel);
				}

				memberAchievement.setAchievementRightNew(memberAchievement
						.getAchievementRightNew() + coutlevel);
				memberAchievement.setAchievementRightTotal(memberAchievement
						.getAchievementRightTotal() + coutlevel);
				location = location / 2;

				memberAchievementDao.update(memberAchievement);

			} else {// 表示是上一级左区
				StringBuilder classLevel = new StringBuilder()
						.append(layer - 1).append("-")
						.append((location + 1) / 2);
				MemberRegisterInfo memberR = memberRegisterInfoDao
						.getMbRegisterByLevel(classLevel.toString());
				MemberAchievement memberAchievement = memberAchievementDao
						.getMemberAchievementByBaseName(memberR
								.getBaseUsername());
				// 会员最高可以获取的对碰奖金
				double maxBonus = StokenUtils.getMaxHitBonus(memberAchievement
						.getRegisterGrade());
				// 奖金汇总表
				AchievementList achievmentList = achievemntListdao
						.getAchievemntListByAccountAndDate(
								StokenRule.getCalculateBonusDate(),
								memberR.getBaseUsername());
				// 现金账户余额
				MemberWallet memw = memberWalletDao
						.getMemberWalletByUserName(memberR.getBaseUsername());
				// 获得消费基金账户
				MemberShopWallet memwShop = memberWalletShopDao
						.getMemberWalletByUserName(memberR.getBaseUsername());
				if (memberAchievement.getAchievementLeftTotal() < memberAchievement
						.getAchievementRightTotal()) {
					// 奖金详细列表
					MemberBonusList memberBonusList = new MemberBonusList(); // 奖金详细列表
					memberBonusList.setBaseUsername(memberBaseInfoDao.find(
							"baseUsername", memberR.getBaseUsername()));
					memberBonusList.setBonusBringUsername(memberBaseInfoDao
							.find("baseUsername", mrinfo.getBaseUsername()));
					memberBonusList.setBonusSort(4);
					memberBonusList.setBonusBringTime(new Date());

					int surplusLevel = memberAchievement
							.getAchievementRightTotal()
							- memberAchievement.getAchievementLeftTotal();
					// 该会员已经产生的奖金
					double hitBonusBefore = memberAchievement
							.getAchievementHit()
							* StokenUtils.UNIT
							* StokenUtils.HITBOUNS;
					double actual = 0;
					if (surplusLevel - coutlevel >= 0) {

						double hitBonusOnes = StokenUtils.UNIT * coutlevel
								* StokenUtils.HITBOUNS; // 改成会员产生的对碰奖金
						// 判断对碰奖金是否封顶
						if (hitBonusBefore < maxBonus) {
							if (hitBonusOnes <= (maxBonus - hitBonusBefore)) {
								actual = hitBonusOnes;
								memwShop.setShopWalletBalance(memwShop
										.getShopWalletBalance()
										+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
								memw.setWalletRemain(memw.getWalletRemain()
										+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
								// 计算领导奖金
								calculationLeadershipBonus(
										memberR,
										(actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
							} else {
								actual = maxBonus - hitBonusBefore;
								memwShop.setShopWalletBalance(memwShop
										.getShopWalletBalance()
										+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
								memw.setWalletRemain(memw.getWalletRemain()
										+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
								// 计算领导奖金
								calculationLeadershipBonus(
										memberR,
										(actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
							}

							achievmentList.setHillBonus(achievmentList
									.getHillBonus() + actual);
							achievmentList
									.setConsumptionBonus(achievmentList
											.getConsumptionBonus()
											+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
							achievmentList
									.setActualBonus(achievmentList
											.getActualBonus()
											+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));

						} else {// 封顶

						}

						memberAchievement.setAchievementHit(memberAchievement
								.getAchievementHit() + coutlevel);

						memberBonusList.setBonusBringMoney(StokenUtils.UNIT
								* coutlevel * StokenUtils.HITBOUNS);
						memberBonusList.setWalletRemain(memw.getWalletRemain());

						memberAchievement
								.setAchievementRightRemain(surplusLevel
										- coutlevel);
					} else {

						double hitBonusOnes = StokenUtils.UNIT * surplusLevel
								* StokenUtils.HITBOUNS; // 改成会员产生的对碰奖金
						// 判断对碰奖金是否封顶
						if (hitBonusBefore < maxBonus) {
							if (hitBonusOnes <= (maxBonus - hitBonusBefore)) {
								actual = hitBonusOnes;
								memwShop.setShopWalletBalance(memwShop
										.getShopWalletBalance()
										+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
								memw.setWalletRemain(memw.getWalletRemain()
										+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
								// 计算领导奖金
								calculationLeadershipBonus(
										memberR,
										(actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
							} else {
								actual = maxBonus - hitBonusBefore;
								memwShop.setShopWalletBalance(memwShop
										.getShopWalletBalance()
										+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
								memw.setWalletRemain(memw.getWalletRemain()
										+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
								// 计算领导奖金
								calculationLeadershipBonus(
										memberR,
										(actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));
							}

							achievmentList.setHillBonus(achievmentList
									.getHillBonus() + actual);
							achievmentList
									.setConsumptionBonus(achievmentList
											.getConsumptionBonus()
											+ (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE));
							achievmentList
									.setActualBonus(achievmentList
											.getActualBonus()
											+ (actual - (actual * StokenRule.CONSUMPTION_WITHDRAWALSFEE)));

						} else {// 封顶

						}

						memberAchievement.setAchievementHit(memberAchievement
								.getAchievementHit() + surplusLevel);
						memberBonusList.setBonusBringMoney(StokenUtils.UNIT
								* surplusLevel * StokenUtils.HITBOUNS);
						memberBonusList.setWalletRemain(memw.getWalletRemain());

						memberAchievement.setAchievementRightRemain(0);
						memberAchievement.setAchievementLeftRemain(coutlevel
								- surplusLevel);
					}
					memberBonusListDao.save(memberBonusList);
					achievemntListdao.update(achievmentList);
					memberBonusListDao.save(memberBonusList);
					memberWalletDao.update(memw);
					memberWalletShopDao.update(memwShop);

				} else if (memberAchievement.getAchievementLeftTotal() >= memberAchievement
						.getAchievementRightTotal()) {
					memberAchievement
							.setAchievementLeftRemain(memberAchievement
									.getAchievementLeftRemain() + coutlevel);
				}

				memberAchievement.setAchievementLeftNew(memberAchievement
						.getAchievementLeftNew() + coutlevel);
				memberAchievement.setAchievementLeftTotal(memberAchievement
						.getAchievementLeftTotal() + coutlevel);
				location = (location + 1) / 2;
				memberAchievementDao.update(memberAchievement);
			}
			layer = (layer - 1);
		}
		;
		mrinfo.setPremium(1); // 表示已经计算了奖金
		memberRegisterInfoDao.update(mrinfo);

	}

	@Override
	public void calculateSellBonus(MemberRegisterInfo memberRegisterInfo) {
		// 计算直接推荐奖金
		calculationSellBonus(memberRegisterInfo);
	}

	@Override
	public void calculateHitBonus() {
		// 计算领导奖金
		countActiveMembers();

	}

	@Override
	public void initdata() {
		// 初始化数据(更新业绩表)
		memberAchievementDao.initMemberAchivementByEveyOne();
		List<MemberRegisterInfo> activeds = memberRegisterInfoDao
				.getActivedMembers();
		// 初始化业绩统计表
		for (MemberRegisterInfo memberRegi : activeds) {
			achievemntListdao.initMemberAchievementInfo(memberRegi);
		}

	}

	@Override
	public void starSystemService() throws SchedulerException, ParseException {
		
		System.out.println("启动服务");
		SchedulerFactory sf = new StdSchedulerFactory();

		Scheduler sched = sf.getScheduler();
		/*
		 * JobDetail job = new JobDetail("caculationHitJob",
		 * "caculationJobGroup", CalculationHitJob.class); CronTrigger trigger =
		 * new CronTrigger("trigger1", "caculationJobGroup", "caculationHitJob",
		 * "caculationHitJob", "0/20 * * * * ?");
		 */
		JobDetail honusJob = new JobDetail("job1", "group1",
				CalculationHitJob.class);
		CronTrigger honusTrigger = new CronTrigger("trigger1", "group1",
				"job1", "group1", "0 0/2 * * * ?");

		JobDetail systemJob = new JobDetail("job2", "group2",
				SystemInitJob.class);
		CronTrigger systemTrigger = new CronTrigger("trigger2", "group2",
				"job2", "group2", "0 0 0 * * ?");

		sched.scheduleJob(systemJob, systemTrigger);
		sched.scheduleJob(honusJob, honusTrigger);
		sched.start();
		System.out.println("启动服务结束");

	}

}
